<template>
    <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: 'home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>课程信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="serch">
        <el-input style="max-width: 600px" placeholder="输入搜索信息" v-model.trim="serchinfo">
            <template #prepend>
                <el-select placeholder="选择信息" v-model="select" style="width: 115px">
                    <el-option label="课程名称" value="cname" />
                    <el-option label="课程编号" value="courseid" />
                    <el-option label="课程类型" value="type" />
                </el-select>
            </template>
            <template #append>
                <el-button @click="seach"><el-icon>
                        <Search />
                    </el-icon></el-button>
            </template>
        </el-input>
    </div>
    <div class="data">
        <p style="margin-left:10px ;">课程信息</p>
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="cname" label="课程名称" width="180" />
            <el-table-column prop="courseid" label="课程编号" width="180" />
            <el-table-column prop="type" label="课程类型" />
            <el-table-column prop="ctime" label="学时" />
            <el-table-column prop="credit" label="学分" />
            <el-table-column prop="teacher" label="授课教师" />
            <el-table-column label="操作" min-width="120" v-if="userstore.userinfo.roleid==1">
                <template #default>
                    <el-button type="primary" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <el-pagination v-model:current-page="queryinfo.pagenum" v-model:page-size="queryinfo.pagesize"
        :page-sizes="[5, 6, 7, 8]" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
</template>

<script setup lang='ts'>
import { ref, onMounted } from 'vue'
import { getcourselist, seachcourse } from '@/apis/course'
import { ElMessage } from 'element-plus'
// @ts-ignore
import { Userstore } from '@/stores/user.js';
const userstore =Userstore()
const serchinfo = ref<string>('')
const select = ref<string>('')
const tableData = ref([])
const queryinfo = ref({
    pagesize: 5,
    pagenum: 1
})
const total = ref(0)
const courselist = async () => {
    const res = await getcourselist({ condition: select.value, val: serchinfo.value, ...queryinfo.value })
    tableData.value = res.data.course
    total.value = res.data.total
}
const handleSizeChange = (val: any) => {
    changepage()
}
const handleCurrentChange = (val: any) => {
    changepage()
}
const changepage =()=>{
    if(serchinfo.value == '' || select.value == ''){ courselist()}
    else {
        seachcourselist()
    }

}
const seachcourselist = async () => {
     const res = await seachcourse({ condition: select.value, val: serchinfo.value, ...queryinfo.value })
    tableData.value = res.data.course
    total.value = res.data.total
}
const seach = async () => {
    if (serchinfo.value == '' || select.value == '') return ElMessage({ type: 'warning', message: '请输入搜索的值' })
    seachcourselist()
}
onMounted(() => {
    courselist()
})
</script>
<style lang='scss' scoped>
.serch {
    margin-top: 15px;

    .el-button {
        color: #fff;
        background-color: #43a0ff;
    }
}

.data {
    margin-top: 10px;
    background-color: #fff;
}

:deep(.el-input__wrapper) {
    padding: 0;
    border-radius: 0;
}

:deep(.el-input__inner) {
    outline: 0;
    padding: 0 10px;
    border: 0;
}
</style>